package com.tk.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tk.util.*;
import com.tk.entity.*;

public class RealeaseTheme extends HttpServlet {

	public RealeaseTheme() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request,response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Hero hero = (Hero)request.getSession().getAttribute("hero");
		
		request.setCharacterEncoding("UTF-8");
		
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "insert into post (title,pid,replyTime,lastReply,posterPos,posterName,content) values(?,0,now(),now(),?,?,?)";
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, title);
			ps.setInt(2, hero.getPosnum());
			ps.setString(3, hero.getHname());
			ps.setString(4, content);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if( ps!=null ){
				DBUtil.close(ps);
			}
			DBUtil.close(conn);
		}

		request.getRequestDispatcher("PaginationTheme").forward(request, response);
		
	}

}
